package com.youdoneed.community.shortVideo.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import com.youdoneed.community.shortVideo.entity.Musictype;
import com.youdoneed.community.shortVideo.entity.Musictyperel;
import com.youdoneed.community.shortVideo.service.MusictypeService;
import com.youdoneed.community.shortVideo.vo.MusicTypeRelVo;
import com.youdoneed.community.shortVideo.vo.MusicTypeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 音乐类型控制器
 * @author lixs
 * 2017年8月31日
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/musictype", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "音乐类型模块控制器", description = "音乐类型模块控制器")
public class MusictypeController {

	private final Logger logger = LoggerFactory.getLogger(MusictypeController.class);
	
	@Autowired
	MusictypeService musictypeService;
	
	
	/**
	 * 添加音乐分类
	 * @param musictype
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@ResponseBody
    @RequestMapping(value = "/addMusicType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "添加音乐分类", value = "添加音乐分类")
	public ResultMessage<Integer> addMusicType(Musictype musictype){
		
		logger.info("添加音乐分类");
		int result = musictypeService.addMusicType(musictype);
		
		if(0==result){
			return ResultMessage.newFailure("添加音乐分类失败");
		}
		return ResultMessage.newSuccess("添加音乐分类成功").setData(result);
	}
	
	
	/**
	 * 删除音乐分类
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@ResponseBody
    @RequestMapping(value = "/deleteMusicType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "删除音乐分类", value = "删除音乐分类")
	public ResultMessage<Integer> deleteMusicType(String typeId){
		
		logger.info("删除音乐分类");
		int result = musictypeService.deleteMusicType(typeId);
		
		if(0==result){
			return ResultMessage.newFailure("删除音乐分类失败");
		}
		return ResultMessage.newSuccess("删除音乐分类成功").setData(result);
	}
	
	/**
	 * 修改音乐分类
	 * @param musictype
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@ResponseBody
    @RequestMapping(value = "/updateMusicType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "修改音乐分类", value = "修改音乐分类")
	public ResultMessage<Integer> updateMusicType(Musictype musictype){
		
		logger.info("修改音乐分类");
		int result = musictypeService.updateMusicType(musictype);
		
		if(0==result){
			return ResultMessage.newFailure("修改音乐分类失败");
		}
		return ResultMessage.newSuccess("修改音乐分类成功").setData(result);
	}
	
	/**
	 * 查询所有音乐分类
	 * @param offset
	 * @param limit
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@ResponseBody
    @RequestMapping(value = "/findMusictypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查询所有音乐分类", value = "查询所有音乐分类")
	public ResultMessage<PageData> findMusictypeList(Boolean mark,Integer offset,Integer limit){
		
		logger.info("查询所有音乐分类");
		List<Musictype> musictype =musictypeService.findMusictypeList(mark,offset, limit);
		
		PageData page = new PageData();
		page.setRows(musictype);
		if(true==mark){
		page.setTotal(musictypeService.countMusicType());
		}else{
			page.setTotal(musictypeService.countMusicTypeForPublic()-1);
		}
		return ResultMessage.newSuccess("查询所有音乐分类成功").setData(page);
	}
	
	/**
	 * 查询音乐分类下的音乐
	 * @param typeId
	 * @param offset
	 * @param limit
	 * @author lixs
	 * @Date 2017.9.1
	 */
	@ResponseBody
    @RequestMapping(value = "/findTypeMusic", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查询音乐分类下的音乐", value = "查询音乐分类下的音乐")
	public ResultMessage<PageData> findTypeMusic(String typeId,Integer offset,Integer limit){
		
		logger.info("查询音乐分类下的音乐");
		List<Musiclibrary> musiclibrary = musictypeService.findTypeMusic(typeId, offset, limit);
		
		PageData page = new PageData();
		page.setRows(musiclibrary);
		page.setTotal(musictypeService.findTypeMusicForTotal(typeId));
		
		return ResultMessage.newSuccess("查询音乐分类下的音乐").setData(page);
		
	}
	
	/**
	 * 通过名字模糊查询音乐分类下的音乐
	 * @param typeId
	 * @param name
	 * @param offset
	 * @param limit
	 * @author lixs
	 * @Date 2017.9.18
	 */
	@ResponseBody
    @RequestMapping(value = "/findTypeMusicByName", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "通过名字模糊查询音乐分类下的音乐", value = "通过名字模糊查询音乐分类下的音乐")
	public ResultMessage<PageData> findTypeMusicByName(String typeId,String name,Integer offset,Integer limit){
		
		logger.info("通过名字模糊查询音乐分类下的音乐");
		List<Musiclibrary> musiclibrary = musictypeService.findTypeMusicByName(typeId, name, offset, limit);
		
		PageData page = new PageData();
		page.setRows(musiclibrary);
		page.setTotal(musictypeService.findTypeMusicByNameForTotal(typeId, name));
		return ResultMessage.newSuccess("通过名字模糊查询音乐分类下的音乐").setData(page);
	}
	
	/**
	 * 向音乐分类下批量添加音乐
	 * @param vo
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.18
	 */
	@ResponseBody
    @RequestMapping(value = "/addMusicToTypeBatch", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "向音乐分类下批量添加音乐", value = "向音乐分类下批量添加音乐")
	public ResultMessage<Boolean> addMusicToTypeBatch(@RequestBody MusicTypeVo vo){
		
		logger.info("向音乐分类下批量添加音乐");
		List<String> musicId = vo.getMusicId();
		String typeId = vo.getTypeId();
		if(null==musicId){
			return ResultMessage.newFailure("向音乐分类下批量添加音乐失败");
		}
		Boolean result = musictypeService.addMusicToTypeBatch(musicId, typeId);
		if(false==result){
			return ResultMessage.newFailure("向音乐分类下批量添加音乐失败");
		}
		return ResultMessage.newSuccess("向音乐分类下批量添加音乐成功");
	}

	/**
	 * 向音乐分类下批量删除音乐
	 * @param vo
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.18
	 */
	@ResponseBody
    @RequestMapping(value = "/deleteMusicToTypeBatch", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "向音乐分类下批量删除音乐", value = "向音乐分类下批量删除音乐")
	public ResultMessage<Boolean> deleteMusicToTypeBatch(@RequestBody MusicTypeRelVo vo){
		
		logger.info("向音乐分类下批量删除音乐");
		List<Musictyperel> musictyperel = vo.getMusictyperel();
		if(null==musictyperel){
			return ResultMessage.newFailure("向音乐分类下批量删除音乐失败");
		}
		Boolean result = musictypeService.deleteMusicToTypeBatch(musictyperel);
		if(false==result){
			return ResultMessage.newFailure("向音乐分类下批量删除音乐失败");
		}
		return ResultMessage.newSuccess("向音乐分类下批量删除音乐成功");
	}
	
	/**
	 * 在音乐分类下将音乐复制到另一音乐分类中
	 * @param musicId
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.23
	 * 
	 */
	@ResponseBody
    @RequestMapping(value = "/copyMusicToAnotherType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "在音乐分类下将音乐复制到另一音乐分类中", value = "在音乐分类下将音乐复制到另一音乐分类中")
	public ResultMessage<Boolean> copyMusicToAnotherType(@RequestBody MusicTypeVo vo){
		logger.info("在音乐分类下将音乐复制到另一音乐分类中");
		List<String> musicId = vo.getMusicId();
		String typeId = vo.getTypeId();
		Boolean result = musictypeService.copyMusicToAnotherType(musicId, typeId);
		if(true==result){
			return ResultMessage.newSuccess("在音乐分类下将音乐复制到另一音乐分类中成功");
		}
		return ResultMessage.newFailure("在音乐分类下将音乐复制到另一音乐分类中失败");
	}
	
	/**
	 * 将音乐剪切到另一音乐分类中
	 * @param vo
	 * @param typeId
	 * @author lixs
	 * @Date 2017.9.25
	 */
	@ResponseBody
    @RequestMapping(value = "/cutMusicToAnotherType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "将音乐剪切到另一音乐分类中", value = "将音乐剪切到另一音乐分类中")
	public ResultMessage<Boolean> cutMusicToAnotherType(@RequestBody MusicTypeRelVo vo){
		logger.info("将音乐剪切到另一音乐分类中");
		List<Musictyperel> musictyperel = vo.getMusictyperel();
		String typeId = vo.getTypeId();
		Boolean result = musictypeService.cutMusicToAnotherType(musictyperel, typeId);
		if(true==result){
			return ResultMessage.newSuccess("将音乐剪切到另一音乐分类中成功");
		}
		return ResultMessage.newFailure("将音乐剪切到另一音乐分类中失败");
	}
}
